package org.example.domain.trade.service.lock.filter;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.trade.adapter.repository.ITradeRepository;
import org.example.domain.trade.model.entity.GroupBuyActivityEntity;
import org.example.domain.trade.model.entity.TradeLockRuleCommandEntity;
import org.example.domain.trade.model.entity.TradeLockRuleFilterBackEntity;
import org.example.domain.trade.service.lock.factory.TradeLockRuleFilterFactory;
import org.example.types.design.framework.link.model2.Handler.ILogicHandler;
import org.example.types.enums.ActivityStatusEnumVO;
import org.example.types.enums.ResponseCode;
import org.example.types.exception.AppException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service

public class ActivityUsabilityRuleFilter implements ILogicHandler<TradeLockRuleCommandEntity, TradeLockRuleFilterFactory.DynamicContext, TradeLockRuleFilterBackEntity>
{
@Resource
    private ITradeRepository repository;

    @Override
    public TradeLockRuleFilterBackEntity apply(TradeLockRuleCommandEntity requestParameter, TradeLockRuleFilterFactory.DynamicContext dynamicContext) throws Exception {
        log.info("交易规则过滤-活动可用性校验:{} activityId:{}",requestParameter.getUserId(),requestParameter.getActivityId());

       GroupBuyActivityEntity groupBuyActivity=repository.queryGroupBuyActivityByActivityId(requestParameter.getActivityId());

       //校验 活动状态
       if(!ActivityStatusEnumVO.EFFECTIVE.equals(groupBuyActivity.getStatus()))
       {
           log.info("活动的可用性校验，非生效状态 activityId:{}", requestParameter.getActivityId());
           throw new AppException(ResponseCode.E0101.getCode(),ResponseCode.E0101.getInfo());
       }
       //校验：活动时间
        Date currentTime=new Date();
        if(currentTime.before(groupBuyActivity.getStartTime())||currentTime.after(groupBuyActivity.getEndTime()))
        {
            log.info("活动的可用性校验，非可参与时间范围 activityId:{}", requestParameter.getActivityId());
            throw new AppException(ResponseCode.E0102.getCode(),ResponseCode.E0102.getInfo());
        }

        dynamicContext.setGroupBuyActivity(groupBuyActivity);
       return next(requestParameter,dynamicContext);
    }
}
